import {fetch} from '@tauri-apps/plugin-http';
import {BizResult} from "@/entiy/vo/BizResult.ts";
import {BizResultUtils} from "@/utils/BizResultUtils.ts";
import {ApiResponse} from "@/entiy/vo/ApiResponse.ts";
import PromiseUtils from "@/utils/PromiseUtils.ts";

class HttpService {
    public static async post<T>(url: string, data?: any): Promise<BizResult<T>> {
        try {
            const response = await fetch("http://119.23.144.149:8090/tools" + url, {
                method: "POST",
                body: JSON.stringify(data),
                headers: {
                    "Content-Type": "application/json",
                },
                connectTimeout: 60000,
            });

            // 添加错误处理
            try {
                const data: ApiResponse<T> = await response.json();
                console.log(data);
                // return BizResultUtils.handleResult(data);
                return PromiseUtils.rejectSystemError()
            } catch (parseError) {
                console.error("接口解析失败:", parseError);
                return BizResultUtils.systemError();
            }
        } catch (e) {
            console.log("接口调用失败:", e);
            return BizResultUtils.systemError();
        }
    }
}

export default HttpService;